package com.neuedu.security.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neuedu.security.mapper.ArticleMapper;
import com.neuedu.security.model.Article;
import com.neuedu.security.model.RespBean;
import com.neuedu.security.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Collection;
import java.util.List;

@Service
@Transactional
public class ArticleService {

    @Autowired
    ArticleMapper articleMapper;

    public RespBean addArticle(Article article) {
        User user = ((User) SecurityContextHolder.getContext().getAuthentication().getPrincipal());
        article.setUid(user.getId());
        int num = articleMapper.addArticle(article);
        if (num == 0) {
            return RespBean.fail(500, "添加文章失败");
        } else {
            return RespBean.success(200, "新增文章成功！", article.getId());
        }
    }

    public RespBean updateArticle(Article article) {
        int num = articleMapper.updateArticle(article);
        if (num == 0) {
            return RespBean.fail(500, "更新文章失败");
        } else {
            return RespBean.success(200, "更新文章成功！");
        }
    }

    public RespBean getArticle(int id) {
        int num = articleMapper.getArticle(id);
        RespBean respBean = null;
        if (num > 0) {
            respBean = RespBean.fail(500, "请先删除栏目下的所有文章！");
        } else {
            respBean = RespBean.success(200, null);
        }
        return respBean;
    }

    public PageInfo getArticlesByStatus(int status, String titleKeyWords, int pageNumber, int pageSize) {
        // 开启分页
        PageHelper.startPage(pageNumber, pageSize);
        User user = ((User) SecurityContextHolder.getContext().getAuthentication().getPrincipal());
        Collection<? extends GrantedAuthority> authorities = user.getAuthorities();
        for (GrantedAuthority authority : authorities) {
            if (authority.getAuthority().equals("ROLE_admin")) {
                List<Article> articleList = articleMapper.getArticlesByStatus(status, user.getId(), null, titleKeyWords);
                PageInfo pageInfo = new PageInfo(articleList);
                return pageInfo;
            }
        }
        List<Article> articleList = articleMapper.getArticlesByStatus(status, user.getId(), user.getId(), titleKeyWords);
        PageInfo pageInfo = new PageInfo(articleList);
        return pageInfo;
    }

    /*判断点击编辑的用户是否为当前登录用户 或 管理员*/
    public RespBean judgeAdminOrUser(int uid) {
        /*当前登录用户*/
        User user = ((User) SecurityContextHolder.getContext().getAuthentication().getPrincipal());
        /*当前登录用户的id*/
        int loginUserId = user.getId();
        Collection<? extends GrantedAuthority> authorities = user.getAuthorities();
        RespBean respBean = null;
        for (GrantedAuthority authority : authorities) {
            if (authority.getAuthority().equals("ROLE_admin") || loginUserId == uid) {
                /*当前用户是管理员或者当前登录用户是作者*/
                respBean = RespBean.success(200, "当前用户可以编辑该文章！");
            } else {
                respBean = RespBean.success(500, "当前用户不可以编辑该文章！");
            }
        }
        return respBean;
    }

    public Article getArticleContent(int id) {
        return articleMapper.getArticleContent(id);
    }

    public RespBean recoverArticle(Article article) {
        int num = articleMapper.recoverArticle(article);
        RespBean respBean = null;
        if (num == 0) {
            if (article.getStatus() == null) {
                respBean = RespBean.fail(500, "还原文章失败！");
            } else {
                respBean = RespBean.fail(500, "删除文章失败！");
            }
        } else {
            if (article.getStatus() == null) {
                respBean = RespBean.success(200, "还原文章成功！");
            } else {
                respBean = RespBean.success(200, "文章已保存至回收站！");
            }
        }
        return respBean;
    }

    public RespBean delArticleById(int id) {
        int num = articleMapper.delArticleById(id);
        if (num == 0) {
            return RespBean.fail(500, "删除文章失败");
        } else {
            return RespBean.success(200, "删除文章成功！");
        }
    }

    public Article getArticleDetailsById(int id) {
        Article article = articleMapper.getArticleDetailsById(id);
        if(article!=null){
            articleMapper.updateViews(id);
        }
        return article;
    }
}
